//
//  TestController.m
//  shike
//
//  Created by RegentSoft on 2017/4/24.
//  Copyright © 2017年 RegentSoft. All rights reserved.
//

#import "TestController.h"
#import "TestTableViewCell.h"
@interface TestController ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView *testTabView;
    NSArray * array;
    NSInteger  indexInteger;
    NSInteger  secondsCountDown; //考试时间
    UILabel * numlabel;          //当前考试的题目
    UILabel * timelabel;

}
@property (strong, nonatomic) NSMutableArray * answerArray;//存放考试答案
@property (strong, nonatomic) NSTimer * countDownTimer;//倒计时
@property (assign, nonatomic) BOOL  hidden;//当前题目选择的答案
@end
@implementation TestController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"技巧销售考核";
    self.view.backgroundColor = [UIColor whiteColor];
    indexInteger = 0;
    secondsCountDown = 120;
    array =[[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F", nil];
    UIBarButtonItem * barItem =[[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(submitClic)];
    barItem.tintColor = [UIColor colorWithHexString:@"#999999"];
    self.navigationItem.rightBarButtonItem = barItem;

    UIBarButtonItem * leftItem =[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(leftClick)];
    barItem.tintColor = [UIColor colorWithHexString:@"#999999"];
    self.navigationItem.leftBarButtonItem = leftItem;
    [self creatView];
}
-(void)leftClick{
}
-(void)creatView{
    
    UIView * headView =[[UIView alloc] initWithFrame:CGRectMake(0,0, DeviceWidth, DeviceHeight-50)];
    headView.backgroundColor = [UIColor colorWithHexString:mainBgColor];
    [self.view addSubview:headView];
    testTabView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, DeviceWidth, DeviceHeight-200) style:UITableViewStylePlain];
    testTabView.delegate = self;
    testTabView.dataSource = self;
    testTabView.contentOffset = CGPointMake(DeviceWidth*3,DeviceHeight-50);
    testTabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [headView addSubview:testTabView];
    UIView * footView =[[UIView alloc] initWithFrame:CGRectMake(0, DeviceHeight-50, DeviceWidth, 50)];
    footView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:footView];
    
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, 1)];
    lineView.backgroundColor = [UIColor blackColor];
    lineView.alpha = 0.3;
    [footView addSubview:lineView];
    
    UIButton * nextButton =[[UIButton alloc] initWithFrame:CGRectMake(20, 10, 70, 30)];
    nextButton.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    [nextButton setTitleColor:[UIColor colorWithHexString:@"#c8c8c8"] forState:UIControlStateNormal];
    nextButton.alpha = 1.0;
    [nextButton setTitle:@"上一题" forState:UIControlStateNormal];
    nextButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    nextButton.tag = 1000;
    nextButton.layer.borderWidth = 1.0;
    nextButton.layer.borderColor = [[UIColor colorWithHexString:@"#979797"] CGColor];
    nextButton.layer.cornerRadius = 8.0;
    [nextButton addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:nextButton];
    
    UIButton * intupButton =[[UIButton alloc] initWithFrame:CGRectMake(DeviceWidth-90, 10, 70, 30)];
    intupButton.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    [intupButton setTitleColor:[UIColor colorWithHexString:@"#c8c8c8"] forState:UIControlStateNormal];
    [intupButton setTitle:@"下一题" forState:UIControlStateNormal];
    intupButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    intupButton.layer.borderWidth = 1.0;
    intupButton.layer.borderColor = [[UIColor colorWithHexString:@"#979797"] CGColor];
    intupButton.layer.cornerRadius = 8.0;
    intupButton.tag = 1001;
    [intupButton addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:intupButton];
    
    numlabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 15,40,20)];
    numlabel.text = @"10/12";
    numlabel.textColor = [UIColor colorWithHexString:@"#323232"];
    numlabel.font = [UIFont systemFontOfSize:16.0];
    numlabel.textAlignment = NSTextAlignmentCenter;
    [footView addSubview:numlabel];
    
    timelabel = [[UILabel alloc] initWithFrame:CGRectMake(DeviceWidth-200, 15, 100, 20)];
    timelabel.text = @"剩余：2分钟";
    timelabel.textColor = [UIColor colorWithHexString:@"#323232"];
    timelabel.font = [UIFont systemFontOfSize:16.0];
    timelabel.textAlignment = NSTextAlignmentCenter;
    [footView addSubview:timelabel];
    [self openTimer];

}
-(void)nextBtnClick:(UIButton *)sender{


}
-(void)submitClic{

}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSString * titleString;
    NSString * numberString;
    if (section==0) {
        titleString = @"题目(单选题)";
        numberString= @"10";
    }else{
        titleString = @"选项";
    }
    UIView * titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, 30)];
    titleView.backgroundColor =[UIColor whiteColor];
    UILabel * titleLabel =[[UILabel alloc] initWithFrame:CGRectMake(20, 5, 100, 20)];
    titleLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.font = [UIFont systemFontOfSize:12.0];
    titleLabel.text = titleString;
    [titleView addSubview:titleLabel];
    
    UILabel * numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(DeviceWidth-40, 5, 20, 20)];
    numberLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    numberLabel.textAlignment = NSTextAlignmentCenter;
    numberLabel.font = [UIFont systemFontOfSize:12.0];
    numberLabel.text = numberString;
    [titleView addSubview:numberLabel];
    return titleView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 80;
    }else{
        return 40;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ==0) {
        return 1;
    }else{
        return 4;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellIdenifer = @"idenifer";
    TestTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdenifer];
    if (!cell) {
        cell =[[TestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdenifer];
    }
    if (indexPath.section==1) {
        cell.titleLabel.text = [NSString stringWithFormat:@"%@",[array objectAtIndex:indexPath.row]];
        cell.selectBtn.tag = indexPath.row+1000;
        cell.selectBtn.selected = NO;
        //单选的做法
        if (cell.selectBtn.tag == indexInteger) {
            [cell.selectBtn setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
            NSMutableDictionary * answerDic = [[NSMutableDictionary alloc] init];
            NSNumber *boolNumber = [NSNumber numberWithBool:_hidden];
            [answerDic setValue:boolNumber forKey:@"cell.contentLabel.text"];
            [_answerArray addObject:answerDic];
        }else{
            [cell.selectBtn setImage:[UIImage imageNamed:@"unSelect"] forState:UIControlStateNormal];
        }
        cell.contentLabel.text = @"销售技巧考核";
        [cell.selectBtn addTarget:self action:@selector(chooseBtn:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        cell.selectBtn.hidden = YES;
        cell.headLabel.text = @"1.歌莉娅是在哪年创立的？歌莉娅是在哪年创立的？歌莉娅是在哪年创立的？歌莉娅是在哪年创立的？歌莉娅是在哪年创立的？歌莉娅是在哪年创立的？歌莉娅是在哪年创立的？";
    }
    return cell;
}
-(void)chooseBtn:(UIButton *)sender{
    indexInteger =sender.tag;
    [testTabView reloadData];
}

-(void)openTimer{
    [_countDownTimer invalidate];
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
}
-(void)timeFireMethod{
    secondsCountDown--;
    if(secondsCountDown<=0){
        //关闭定时器
        [_countDownTimer invalidate];
    }else{
        NSInteger minutes = secondsCountDown / 60;
        NSString *stringTime = [NSString stringWithFormat:@"剩余: %ld分",(long)minutes];
        timelabel.text = stringTime;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}
@end
